package com.softer.chengxin.corp.dao;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.corp.dao.provider.ManagerDaoProvider;
import com.softer.chengxin.corp.domin.*;
import com.softer.chengxin.person.domin.Person;
import com.softer.chengxin.person.domin.PersonVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

/**
 * @author wangxinrui
 * @date 2018/10/09
 */
@Mapper
@Repository
public interface ManagerDao {
    /**
     * 查询企业主要人员信息
     *
     * @param qyId   企业id
     * @param duties 职位
     * @return 人员信息
     */
    @Select("select id as id,DATE_FORMAT(gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(gmt_modified, '%Y-%m-%d') as gmtModified,person_id as personId,project_id as projectId,company_id as companyId,department_id as departmentIdCode,(SELECT name FROM dic_info WHERE code = department_id) as departmentId,`name` as `name`,sex as sex,country_code as countryCode,birthday as birthday,education_level as educationLevelCode,(SELECT name FROM dic_info WHERE code = education_level) AS educationLevel,id_card as idCard,photo as photo,political_status as politicalStatus,phone as phone,major AS majorCode,(SELECT name FROM dic_info WHERE code = major) AS major,insurance as insurance,duties AS dutiesCode,(SELECT name FROM dic_info WHERE code = duties) AS duties,state as state,certificate as certificate,id_card_file_id as idCardFileId,certificate_file_id as certificateFileId,social_security_file_id as socialSecurityFileId,labor_contract_file_id as laborContractFileId,diploma_file_id as diplomaFileId " +
            "from company_person where company_id=#{qyId} and duties=#{duties} and state=1")
    Person findZYRY(@Param("qyId") String qyId,
                    @Param("duties") Integer duties);

    /**
     * 查询企业从业人员信息 条件查询
     *
     * @param qyId  企业id
     * @param page  页码
     * @param name  人员姓名
     * @param depId 部门id
     * @return 人员信息
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findCYRY")
    List<PersonVO> findCYRY(@Param("qyId") String qyId,
                            @Param("page") Page page,
                            @Param("name") String name,
                            @Param("depId") String depId);

    /**
     * 查询企业从业人员信息条数
     *
     * @param qyId  企业id
     * @param name  人员姓名
     * @param depId 部门id
     * @return 人员信息
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findCYRYCount")
    int findCYRYCount(@Param("qyId") String qyId,
                      @Param("name") String name,
                      @Param("depId") String depId);

    /**
     * 条件查询企业信息
     *
     * @param qyName      企业名
     * @param district    地区
     * @param cxDateBegin 查询开始时间
     * @param cxDateEnd   查询结束时间
     * @param state       状态
     * @param page        页码
     * @param area        管理部门管理的区域
     * @return 企业信息
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findCorpByCondition")
    List<Corp> findCorpByCondition(@Param("qyName") String qyName,
                                   @Param("district") Integer district,
                                   @Param("cxDateBegin") String cxDateBegin,
                                   @Param("cxDateEnd") String cxDateEnd,
                                   @Param("state") Integer state,
                                   @Param("page") Page page,
                                   @Param("area") String area);

    /**
     * 条件查询企业信息 条数
     *
     * @param qyName      企业名
     * @param district    地区
     * @param cxDateBegin 查询开始时间
     * @param cxDateEnd   查询结束时间
     * @param state       状态
     * @param area        管理部门管理的区域
     * @return 企业信息
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findCorpByConditionCount")
    int findCorpByConditionCount(@Param("qyName") String qyName,
                                 @Param("district") Integer district,
                                 @Param("cxDateBegin") String cxDateBegin,
                                 @Param("cxDateEnd") String cxDateEnd,
                                 @Param("state") Integer state,
                                 @Param("area") String area);

    /**
     * 通过登录名查找主管部门信息
     *
     * @param name 登录名
     * @return 信息
     */
    @Select("select * from manager_user where login_name = #{name}")
    ManagerUser findByName(@Param("name") String name);

    /**
     * 从业人员查询  条件查询
     *
     * @param area   区域
     * @param name   姓名
     * @param qyName 企业名
     * @param page   页码
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findPersonByCondition")
    List<PersonVO> findPersonByCondition(@Param("area") String area,
                                         @Param("name") String name,
                                         @Param("qyName") String qyName,
                                         @Param("page") Page page);

    /**
     * 从业人员查询  条件查询  条数
     *
     * @param area   区域
     * @param name   姓名
     * @param qyName 企业名
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findPersonByConditionCount")
    int findPersonByConditionCount(@Param("area") String area,
                                   @Param("name") String name,
                                   @Param("qyName") String qyName);

    /**
     * 查询企业继续教育情况
     *
     * @param page   页码
     * @param corpId 企业id
     * @param name   姓名
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findTrain")
    List<TrainVO> findTrain(@Param("page") Page page,
                            @Param("corpId") Integer corpId,
                            @Param("name") String name);

    /**
     * 查询企业继续教育情况 条数
     *
     * @param corpId 企业id
     * @param name   姓名
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "findTrainCount")
    int findTrainCount(@Param("corpId") Integer corpId,
                       @Param("name") String name);

    /**
     * 获取企业名 id
     *
     * @param area 区域
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "getCorpNameList")
    List<CorpDTO> getCorpNameList(@Param("area") String area);

    /**
     * 获取项目名 id
     *
     * @param corpId 企业id
     * @return 结果
     */
    @Select("select id ,`project_name` as `name` from company_project where company_id=#{corpId} and state=2")
    List<ProDTO> getProjectNameList(@Param("corpId") Integer corpId);

    /**
     * 通过id查询企业信息
     *
     * @param id 企业id
     * @return 结果
     */
    @Select("select c.id as id,(select `name` from district_street where id=c.district) as districtName,c.three_system as threeSystem, " +
            "(select `name` from district_street where id=c.street) as streetName,DATE_FORMAT(c.gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(c.gmt_modified, '%Y-%m-%d') as gmtModified,c.qy_no as qyNo,c.`name` as `name`,c.pwd as pwd,c.legal_person as legalPerson,c.general_manager as generalManager,c.business_date as businessDate,c.register_type AS registerTypeCode,(SELECT name FROM dic_info WHERE code = c.register_type) AS registerType,c.province as province,c.city as city,c.district as district,c.street as street,c.business_license_no as businessLicenseNo,c.expiration_begin as expirationBegin,c.expiration_end as expirationEnd,c.system_type AS systemTypeCode,(SELECT name FROM dic_info WHERE code = c.system_type) AS systemType,c.registered_capital as registeredCapital,c.main_scope as mainScope,c.sideline as sideline,c.fax as fax,c.post_code as postCode,c.url as url,c.contacts as contacts,c.email as email,c.phone as phone," +
            "c.register_address as registerAddress,c.public_number as publicNumber,c.credit_contract_path as creditContractPath,c.first_contract_path as firstContractPath,c.state as state,c.first_contract_date as firstContractDate,c.business_license_path as businessLicensePath,c.main_business_income as mainBusinessIncome,c.public_income as publicIncome,c.profit as profit,c.organization as organization,c.other_income as otherIncome,c.province_company as provinceCompany,c.company_type as companyType,c.enterprise_qualification as enterpriseQualification,c.qualification_approval_date as qualificationApprovalTime,c.qualification_certificate_no as qualificationCertificateNo,c.qualification_expiration_date as qualificationExpirationTime,c.approval_authority as approvalAuthority,c.national_hundred as nationalHundred,c.famous_brand as famousBrand from company_base_info c where c.id=#{id}")
    Corp getCorpDetail(@Param("id") Integer id);

    /**
     * 审批继续教育
     *
     * @param id     id
     * @param state  状态
     * @param yijian 意见
     * @return 结果
     */
    @Update("update company_train set gmt_modified=now(),state=#{state},opinion=#{yijian} " +
            "where id=#{id}")
    int spTrain(@Param("id") Integer id,
                @Param("state") Integer state,
                @Param("yijian") String yijian);

    /**
     * 获取企业继续教育信息
     *
     * @param area       地区
     * @param qyName     企业名
     * @param state      状态
     * @param page       页码
     * @param department 培训部门
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "getTrainList")
    List<TrainVO> getTrainList(@Param("area") String area,
                               @Param("qyName") String qyName,
                               @Param("state") Integer state,
                               @Param("page") Page page, @Param("department") Integer department);

    /**
     * 获取企业继续教育信息 条数
     *
     * @param area       地区
     * @param qyName     企业名
     * @param state      状态
     * @param department 培训部门
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "getTrainListCount")
    int getTrainListCount(@Param("area") String area,
                          @Param("qyName") String qyName,
                          @Param("state") Integer state, @Param("department") Integer department);

    /**
     * 通过继续教育id查询企业id
     *
     * @param id 继续教育id
     * @return 结果
     */
    @Select("select corp_id from company_train where id=#{id}")
    Integer findCorpId(@Param("id") Integer id);

    /**
     * 通过id查找教育详细信息
     *
     * @param id id
     * @return 结果
     */
    @Select("select id as id,DATE_FORMAT(gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(gmt_modified, '%Y-%m-%d') as gmtModified," +
            "person_id as personId,subject as subject,train_begin_time as trainBeginTime," +
            "state as state,train_end_time as trainEndTime,corp_id as corpId," +
            "department as department,opinion as opinion from company_train where id=#{id}")
    TrainVO findTrainById(@Param("id") Integer id);

    /**
     * 获取该账号登录解绑
     *
     * @param loginName 登录名
     * @return 结果
     */
    @Select("select role_id from manager_user where login_name=#{loginName}")
    Integer getRoleId(@Param("loginName") String loginName);

    /**
     * 继续教育审批按钮  点击后显示的列表
     *
     * @param page       页码
     * @param area       地区
     * @param department 教育培训部门
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "getSPTrainList")
    List<TrainVO> getSPTrainList(@Param("page") Page page,
                                 @Param("department") String department,
                                 @Param("area") String area);

    /**
     * 继续教育审批按钮  点击后显示的列表 条数
     *
     * @param department 教育培训部门
     * @param area       地区
     * @return 结果
     */
    @SelectProvider(type = ManagerDaoProvider.class, method = "getSPTrainListCount")
    int getSPTrainListCount(@Param("department") String department,
                            @Param("area") String area);
}
